package com.esri.core.tasks.tilecache;

import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.internal.tasks.PollingHelper;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.internal.tasks.d;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.ServerFile;
import com.esri.core.map.ServerFolder;
import com.esri.core.tasks.ags.geoprocessing.GPJobResource;
import java.io.File;
import java.net.URL;
import java.util.Map;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes.dex */
class PollAndFetchTileCacheTask extends d<ServerFolder> {
    private static final long serialVersionUID = 1;
    private PollingHelper<TileCacheJobResource, GPJobResource.JobStatus> _pollingHelper;
    private ExportTileCacheStatus _status;

    public PollAndFetchTileCacheTask(ExportTileCacheStatus exportTileCacheStatus, UserCredentials userCredentials) {
        super(null, exportTileCacheStatus.getUrl(), userCredentials);
        this._pollingHelper = new PollingHelper<>();
        this._status = null;
        this._status = exportTileCacheStatus;
    }

    public PollAndFetchTileCacheTask(ExportTileCacheStatus exportTileCacheStatus, UserCredentials userCredentials, TaskListener<ServerFolder> taskListener) {
        super(null, exportTileCacheStatus.getUrl(), userCredentials, taskListener);
        this._pollingHelper = new PollingHelper<>();
        this._status = null;
        this._status = exportTileCacheStatus;
    }

    public void setStatusCallback(ExportTileCacheStatusCallback exportTileCacheStatusCallback, CallbackListener<?> callbackListener) {
        this._pollingHelper.a(exportTileCacheStatusCallback, callbackListener);
    }

    public ExportTileCacheStatus getStatus() {
        return this._status;
    }

    @Override // com.esri.core.internal.tasks.a
    public ServerFolder execute() throws Exception {
        TileCacheJobResource a;
        UserCredentials credentials = getCredentials();
        TileCacheLayerStatusPollHelper tileCacheLayerStatusPollHelper = new TileCacheLayerStatusPollHelper(this._status.getJobID());
        if (!tileCacheLayerStatusPollHelper.isComplete(this._status.getStatus()) && (a = this._pollingHelper.a(5000L, getServiceURL(), credentials, GPJobResource.JobStatus.NEW_JOB, tileCacheLayerStatusPollHelper)) != null) {
            this._status = new ExportTileCacheStatus(a, getServiceURL());
        }
        if (GPJobResource.JobStatus.SUCCEEDED.equals(this._status.getStatus())) {
            TileCacheJobResource tileCacheJobResource = this._status.getTileCacheJobResource();
            String resultUrl = tileCacheJobResource.getResultUrl();
            n serviceCredentials = getServiceCredentials();
            if (tileCacheJobResource.isOnlineTileCache()) {
                ServerFile serverFile = new ServerFile(resultUrl);
                serverFile.setCredentials(credentials);
                ServerFolder serverFolder = new ServerFolder("TileCache");
                serverFolder.add(serverFile);
                return serverFolder;
            }
            JsonParser a2 = h.a(resultUrl, (Map<String, String>) null, serviceCredentials);
            ServerFolder fromJson = ServerFolder.fromJson(a2, new File(new URL(resultUrl).getPath()).getName(), credentials);
            a2.close();
            return fromJson;
        }
        return null;
    }
}
